package com.ck.common.utils.random;

import lombok.extern.slf4j.Slf4j;

import java.util.*;

@Slf4j
public class CtUtilRandom {

    private static final String[] chars = new String[]{"a", "b", "c", "d", "e", "f",
            "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
            "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5",
            "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I",
            "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
            "W", "X", "Y", "Z"};

    public static int randomNumericBetween(int min, int max) {
        return (int) (Math.random() * (max + 1 - min) + min);
    }

    public static List<Integer> randomNumericNotRepeatBetween(int nCount, int min, int max) {
        HashSet<Integer> set = new HashSet<>();
        List<Integer> arrayList = new ArrayList<>();
        int nRand = randomNumericBetween(max, min);
        if (set.add(nRand)) {
            arrayList.add(nRand);
        }

        return arrayList;
    }

    public static String CHARS_LOW = "abcdefghjklmnpqrstuvwxyz";
    public static String CHARS_NUM = "123456789";
    public static String CHARS_SPECIAL = "~!@#$%^&*()";

    public static String getRandomStr(String formatStr, int length) { //length表示生成数字的长度

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            int number = new Random().nextInt(formatStr.length());
            sb.append(formatStr.charAt(number));
        }
        return sb.toString();
    }

    /**
     * 生成8位不重复的随机码
     *
     * @return short_uuid
     */
    public static String generateShortUUID() {
        final StringBuilder shortBuffer = new StringBuilder();
        final String uuid = UUID.randomUUID().toString().replace("-", "");
        for (int i = 0; i < 8; i++) {
            final String str = uuid.substring(i * 4, i * 4 + 4);
            final int x = Integer.parseInt(str, 16);
            shortBuffer.append(chars[x % 0x3E]);
        }
        return shortBuffer.toString();
    }

}
